package com.rongmai.db.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author zhenhua.sun
 * @title com.tailai.tailaishop.common.utils
 * @date 17/6/8
 */
public class ListUtils {

    /**
     * 取交集
     * @param list1
     * @param list2
     * @return
     */
    public static List<String> retainList(List<String> list1,List<String> list2){
        if(list1 == null || list2 == null){
            return null;
        }
        List<String> result = new ArrayList<String>(Arrays.asList(new String[list1.size()]));
        Collections.copy(result,list1);
        result.retainAll(list2);
        return result;
    }

    /**
     * 取差集
     * @param list1
     * @param list2
     * @return
     */
    public static List<String> diffList(List<String> list1,List<String> list2){
        if(list1 == null || list2 == null){
            return null;
        }
        List<String> result = new ArrayList<String>(Arrays.asList(new String[list1.size()]));
        Collections.copy(result,list1);
        result.removeAll(list2);
        return result;
    }

    public static void main(String[] args) {
		List<String> list1 = new ArrayList<String>();
		List<String> list2 = new ArrayList<String>();
        list1.add("111");
        list1.add("222");
        list1.add("333");
        list2.add("333");
        list2.add("444");
        list2.add("555");
        System.out.println(diffList(list1,list2));
        System.out.println(diffList(list2,list1));
        System.out.println(retainList(list2,list1));
    }
}
